package cn.zhaopin.starter.security.convert;

import cn.zhaopin.starter.security.basic.CodeException;
import cn.zhaopin.starter.security.common.SecurityConstant;
import cn.zhaopin.starter.security.user.SecurityContextHolder;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.security.web.server.authentication.ServerAuthenticationConverter;
import org.springframework.util.StringUtils;

/**
 * Description: 基础认证converter
 *
 * @author zuomin (myleszelic@outlook.com)
 * @date: 2021/10/29-15:32
 */
public abstract class BasicAuthenticationConverter implements ServerAuthenticationConverter {

    /**
     * 参数校验
     * @param args 校验参数
     */
    protected void argsCheck(String... args) {
        for (String arg : args) {
            if (!StringUtils.hasLength(arg)) {
                throw CodeException.builder().code(40401).msg("参数为空").build();
            }
        }
    }

    /**
     * 获取参数
     * @param request 请求
     * @param param 参数
     * @return 参数值
     */
    protected String obtainQueryFirst(ServerHttpRequest request, String param) {
        return request.getQueryParams().getFirst(param);
    }

    /**
     * 将路径存储到本地缓存中
     * @param path
     */
    protected void holderPathValue(String path) {
        SecurityContextHolder.setValue(SecurityConstant.PATH_KEY, path);
    }

}
